/*
 * Filename:    OrgReviewController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkUserComfirmDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.IntegrityEvalDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.biz.service.OrgReviewService;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.constants.OrgReviewConstants;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgReviewComfirmDTO;
import com.paic.mock.util.StringUtils;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对定点机构诚信考评审核
 * 
 * @author EX-LUORONG001
 * 
 */
@Controller
@RequestMapping("/orgReview/")
public class OrgReviewController {

	@BizServicesProxy(beanName = "orgReviewServiceImpl")
	private OrgReviewService orgReviewService;

	/**
	 * 根据id加载对应的诚信类别考评详情
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadIntgrityEvalTypeInfo.do")
	@ResponseBody
	public Map<String, Object> loadIntgrityEvalTypeInfo(@ModelAttribute IntegrityEvalDTO intgrityEvalDTO,
			HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> list = this.orgReviewService.getIntgrityEvalTypeInfo(intgrityEvalDTO);
		result.put("flag", OrgReviewConstants.FLAG_TURE);
		result.put("result", list);
		return result;
	}

	/**
	 * 根据id加载对应的诚信考评详情
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadIntgrityEvalDetailInfo.do")
	@ResponseBody
	public Map<String, Object> loadIntgrityEvalDetailInfo(@ModelAttribute EvalMarkUserComfirmDTO eComfirmDTO,
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String ruleId = eComfirmDTO.getEvaluRuleId();
			String evalChildId = eComfirmDTO.getIntgrityEvalChildId();

			Map<String, Object> list = this.orgReviewService.getIntgrityEvalDetail(ruleId, evalChildId);
			result.put("flag", OrgReviewConstants.FLAG_TURE);
			result.put("result", list);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 新增或修改诚信考评意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addEvaluationOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addEvaluationOpinion(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			OrgReviewComfirmDTO opinionDTO = JSONObject.toJavaObject(transferJson, OrgReviewComfirmDTO.class);

			result = this.orgReviewService.processAddOpinion(opinionDTO, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 审核被审批岗位退回来后，审核岗位重新提交的操作
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "auditBackSaveEvaluationOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditBackSaveEvaluationOpinion(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			OrgReviewComfirmDTO opinionDTO = JSONObject.toJavaObject(transferJson, OrgReviewComfirmDTO.class);

			result = this.orgReviewService.auditBackSaveEvaluationOpinion(opinionDTO, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 审核批量提交
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "reviewBatchSumit.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> reviewBatchSumit(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			String auditOpinion = transferJson.get("auditOpinion").toString();
			String auditDesc = transferJson.get("auditDesc").toString();
			String evaluationCodes = transferJson.get("evaluationCodes").toString();
			JSONArray ja = JSON.parseArray(evaluationCodes);
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < ja.size(); i++) {
				String tempStr = ja.getString(i);
				list.add(tempStr);
			}
			if (!StringUtils.hasText(auditOpinion)) {
				result.put(OrgReviewConstants.MSG_KEY, "审核意见不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			if (OrgReviewConstants.opinion_0.equals(auditOpinion) && !StringUtils.hasText(auditDesc)) {
				result.put(OrgReviewConstants.MSG_KEY, "意见描述不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			if (null == list || list.size() <= 0) {
				result.put(OrgReviewConstants.MSG_KEY, "考评选项不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			result = this.orgReviewService.batchSumit(auditOpinion, auditDesc, list, user);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 机构诚信审核撤销
	 */
	@RequestMapping(value = "cancelMedialIntegrity.do")
	@ResponseBody
	public Map<String, Object> cancelMedialIntegrity(HttpServletRequest request) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Map<String, Object> map = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		map.put("evaluationCode", request.getParameter("evaluationCode"));
		map.put("user", user);
		try {
			boolean flag = this.orgReviewService.cancelMedialIntegrity(map);
			returnMap.put("state", flag);
			if (flag) {
				returnMap.put("msg", "操作成功!");
			} else {
				returnMap.put("msg", "该笔记录不能撤销!");
			}
		} catch (Exception e) {
			returnMap.put("state", false);
			returnMap.put("msg", e.getMessage());
			e.printStackTrace();
		}
		return returnMap;
	}

}
